const baseConfig = require("./baseConfig");
const common = require("./common");
const util = require('../../utils/util.js');

let $http = function({
  api = "",
  method = "POST",
  data = {},
  header = {}
}){
  let newHeader;
  //
  try {
    newHeader = common.extend(true,{
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      'shareToken': this.data.userInfo_share.token
    },header);
  } catch (error) {
    newHeader = common.extend(true,{
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      'shareToken': wx.getStorageSync('userInfo_share').token||''
    },header);
  }
  //
  return new Promise((resolve, reject)=>{
    //
    wx.request({
      url: `${baseConfig.Url}${api}`,
      method: method,
      header: newHeader,
      data: util.json2Form(data),
      success: res => {
        //console.log(res);
        resolve(res)
      },
      fail: err => {
        //console.log(err);
        reject(err);
      }
    });
  });
};
//
module.exports = {
  $http
}